﻿'''
'''
''' <summary>
'''
''' </summary>
Public Class Form1

    ''' <summary>
    '''
    ''' </summary>
    '''


    Dim random As New Random()
    Private snakeDirection As Point = New Point(1, 0) ' 初始方向向右
    Private foodLocation As Point
    Private snakeBody As New List(Of Point) From {New Point(5, 5)}

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        foodLocation = New Point(5, 3)
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        ' 移动蛇
        Dim head As Point = snakeBody.Last
        Dim newHead As Point = New Point(head.X + snakeDirection.X, head.Y + snakeDirection.Y)
        snakeBody.Add(newHead)

        ' 检测是否吃到食物
        If newHead = foodLocation Then
            ' 随机生成新食物位置
            foodLocation = New Point(random.Next(1, 20), random.Next(1, 15))
        Else
            snakeBody.RemoveAt(0)
        End If

        ' 绘制
        Refresh()
    End Sub

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        ' 绘制蛇
        For Each part In snakeBody
            e.Graphics.FillRectangle(Brushes.Black, part.X * 10, part.Y * 10, 10, 10)
        Next

        ' 绘制食物
        e.Graphics.FillEllipse(Brushes.Red, foodLocation.X * 10, foodLocation.Y * 10, 10, 10)
    End Sub

    ' 按键处理（简单移动）
    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyCode
            Case Keys.W
                If snakeDirection.Y <> 1 Then snakeDirection = New Point(0, -1)
            Case Keys.S
                If snakeDirection.Y <> -1 Then snakeDirection = New Point(0, 1)
            Case Keys.A
                If snakeDirection.X <> 1 Then snakeDirection = New Point(-1, 0)
            Case Keys.D
                If snakeDirection.X <> -1 Then snakeDirection = New Point(1, 0)
        End Select
    End Sub
End Class